/**
 * ExpressionController.java
 * Created on  2015/4/1 12:08
 * 版本       修改时间         作者        修改内容
 * V1.0.1     2015/4/1     panzhuowen    初始版本
 */
package com.infitecs.eyas.expression;

import com.infitecs.eyas.common.ajax.DataTablesPage;
import com.infitecs.eyas.common.ajax.DataTablesResponse;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * 类的描述信息
 *
 * @author panzhuowen
 * @version 1.0.1
 */
@Controller
@RequestMapping("/admin/formal")
public class ExpressionController {

    @Autowired
    private ExpressionService expressionService;

    @RequestMapping(method = {RequestMethod.GET})
    public String getFormal(HttpServletRequest request) {
        return "expression/expression";
    }

    @RequestMapping("/getFormulaListPage")
    @ResponseBody
    public DataTablesResponse getFormulaListPage(Formula formula,DataTablesPage dataTablesPage) {
        return new DataTablesResponse(dataTablesPage, expressionService.getFormulaListPage(dataTablesPage,formula));
    }

    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
    @ResponseBody
    public AjaxResponse delFormula(@PathVariable int id, HttpSession session) {
        return AjaxResponse.getInstanceByResult(expressionService.delFormula(id), session);
    }

    @RequestMapping(method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResponse addFormula(@RequestBody @Valid Formula formula, HttpSession session) {
        return AjaxResponse.getInstanceByResult(expressionService.addFormula(formula) == Constant.AFFECTED_LINE_1, session);
    }

    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    @ResponseBody
    public AjaxResponse getFormula(@PathVariable int id) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Formula formula = expressionService.getFormula(id);
        ajaxResponse.addAttribute("formula", formula);
        return ajaxResponse;
    }

    @RequestMapping(method = {RequestMethod.PUT})
    @ResponseBody
    public AjaxResponse updateFormula(@RequestBody @Valid Formula formula, HttpSession session) {
        return AjaxResponse.getInstanceByResult(expressionService.updateFormula(formula) == Constant.AFFECTED_LINE_1, session);
    }

    @RequestMapping(value = "checkRepeatFormulaName", method = {RequestMethod.POST})
    @ResponseBody
    public Boolean checkRepeatFormulaName(Formula formula) {
        return expressionService.checkRepeatFormulaName(formula);
    }

    @RequestMapping(value = "/calculate", method = {RequestMethod.POST})
    @ResponseBody
    public Double calculateBurn(Param param) {
        return expressionService.calculate(param);
    }

}
